//package com.lh.config;
//
//import com.alibaba.druid.pool.xa.DruidXADataSource;
//import org.activiti.api.runtime.shared.identity.UserGroupManager;
//import org.activiti.engine.cfg.ProcessEngineConfigurator;
//import org.activiti.engine.impl.bpmn.parser.factory.DefaultActivityBehaviorFactory;
//import org.activiti.engine.impl.persistence.StrongUuidGenerator;
//import org.activiti.spring.SpringAsyncExecutor;
//import org.activiti.spring.SpringProcessEngineConfiguration;
//import org.activiti.spring.boot.AbstractProcessEngineAutoConfiguration;
//import org.activiti.spring.boot.ActivitiProperties;
//import org.activiti.spring.boot.ProcessDefinitionResourceFinder;
//import org.activiti.spring.boot.ProcessEngineConfigurationConfigurer;
//import org.activiti.spring.boot.process.validation.AsyncPropertyValidator;
//import org.activiti.validation.ProcessValidatorImpl;
//import org.activiti.validation.validator.ValidatorSet;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.context.annotation.Primary;
//import org.springframework.core.io.Resource;
//import org.springframework.core.task.TaskExecutor;
//import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
//import org.springframework.transaction.PlatformTransactionManager;
//
//import javax.sql.DataSource;
//import java.io.IOException;
//import java.util.HashSet;
//import java.util.List;
//
//@Configuration
//public class ActivitiDataSourceConfig extends AbstractProcessEngineAutoConfiguration {
//
//    @Value("${spring.datasource.dynamic.datasource.master.password}")
//    protected String password;
//
//    @Value("${spring.datasource.dynamic.datasource.master.driver-class-name}")
//    protected String className;
//
//    @Value("${spring.datasource.dynamic.datasource.master.url}")
//    protected String url;
//
//    @Value("${spring.datasource.dynamic.datasource.master.username}")
//    protected String username;
//
//    public DataSource activitiDataSource() throws Exception {
//        DruidXADataSource dyncds = new DruidXADataSource();
//        dyncds.setDriverClassName(className);
//        dyncds.setUrl(url);
//        dyncds.setUsername(username);
//        dyncds.setPassword(password);
//        dyncds.setDbType("mysql");
//
//        dyncds.setInitialSize(30);
//        dyncds.setMaxActive(500);
//        dyncds.setMinIdle(0);
//        dyncds.setMinEvictableIdleTimeMillis(60000);
//        dyncds.setTimeBetweenConnectErrorMillis(10000);
//        dyncds.setRemoveAbandoned(false);
//        dyncds.setRemoveAbandonedTimeout(200);
//        dyncds.setLogAbandoned(true);
//        dyncds.setMaxWait(60000);
//        dyncds.setFilters("stat");
//        dyncds.setConnectionErrorRetryAttempts(0);
//        dyncds.setBreakAfterAcquireFailure(true);
//        dyncds.setFailFast(true);
//        dyncds.setDefaultAutoCommit(true);
//
//        return dyncds;
//    }
//
//    @Primary
//    @Bean
//    public TaskExecutor primaryTaskExecutor() {
//        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
//        return executor;
//    }
//
//    @Bean
//    @Primary
//    public SpringProcessEngineConfiguration springProcessEngineConfiguration(
//            PlatformTransactionManager transactionManager,
//            SpringAsyncExecutor springAsyncExecutor,
//            ActivitiProperties activitiProperties,
//            ProcessDefinitionResourceFinder processDefinitionResourceFinder,
//            @Autowired(required = false) ProcessEngineConfigurationConfigurer processEngineConfigurationConfigurer,
//            @Autowired(required = false) List<ProcessEngineConfigurator> processEngineConfigurators,
//            UserGroupManager userGroupManager) throws Exception {
//
//        SpringProcessEngineConfiguration conf = new SpringProcessEngineConfiguration();
//        conf.setConfigurators(processEngineConfigurators);
//        configureProcessDefinitionResources(processDefinitionResourceFinder,
//                conf);
//        conf.setDataSource(activitiDataSource());
//        conf.setTransactionManager(transactionManager);
//
//        if (springAsyncExecutor != null) {
//            conf.setAsyncExecutor(springAsyncExecutor);
//        }
//        conf.setDeploymentName(activitiProperties.getDeploymentName());
//        conf.setDatabaseSchema(activitiProperties.getDatabaseSchema());
//        conf.setDatabaseSchemaUpdate("true");
//        conf.setDbHistoryUsed(activitiProperties.isDbHistoryUsed());
//        conf.setAsyncExecutorActivate(activitiProperties.isAsyncExecutorActivate());
//        if (!activitiProperties.isAsyncExecutorActivate()) {
//            ValidatorSet springBootStarterValidatorSet = new ValidatorSet("activiti-spring-boot-starter");
//            springBootStarterValidatorSet.addValidator(new AsyncPropertyValidator());
//            if (conf.getProcessValidator() == null) {
//                ProcessValidatorImpl processValidator = new ProcessValidatorImpl();
//                processValidator.addValidatorSet(springBootStarterValidatorSet);
//                conf.setProcessValidator(processValidator);
//            } else {
//                conf.getProcessValidator().getValidatorSets().add(springBootStarterValidatorSet);
//            }
//        }
//        conf.setMailServerHost(activitiProperties.getMailServerHost());
//        conf.setMailServerPort(activitiProperties.getMailServerPort());
//        conf.setMailServerUsername(activitiProperties.getMailServerUserName());
//        conf.setMailServerPassword(activitiProperties.getMailServerPassword());
//        conf.setMailServerDefaultFrom(activitiProperties.getMailServerDefaultFrom());
//        conf.setMailServerUseSSL(activitiProperties.isMailServerUseSsl());
//        conf.setMailServerUseTLS(activitiProperties.isMailServerUseTls());
//        if (userGroupManager != null) {
//            conf.setUserGroupManager(userGroupManager);
//        }
//
//        conf.setHistoryLevel(activitiProperties.getHistoryLevel());
//        conf.setCopyVariablesToLocalForTasks(activitiProperties.isCopyVariablesToLocalForTasks());
//        conf.setSerializePOJOsInVariablesToJson(activitiProperties.isSerializePOJOsInVariablesToJson());
//        conf.setJavaClassFieldForJackson(activitiProperties.getJavaClassFieldForJackson());
//
//        if (activitiProperties.getCustomMybatisMappers() != null) {
//            conf.setCustomMybatisMappers(
//                    getCustomMybatisMapperClasses(activitiProperties.getCustomMybatisMappers()));
//        }
//
//        if (activitiProperties.getCustomMybatisXMLMappers() != null) {
//            conf.setCustomMybatisXMLMappers(
//                    new HashSet<>(activitiProperties.getCustomMybatisXMLMappers()));
//        }
//
//        if (activitiProperties.getCustomMybatisXMLMappers() != null) {
//            conf.setCustomMybatisXMLMappers(
//                    new HashSet<>(activitiProperties.getCustomMybatisXMLMappers()));
//        }
//
//        if (activitiProperties.isUseStrongUuids()) {
//            conf.setIdGenerator(new StrongUuidGenerator());
//        }
//
//        if (activitiProperties.getDeploymentMode() != null) {
//            conf.setDeploymentMode(activitiProperties.getDeploymentMode());
//        }
//
//        conf.setActivityBehaviorFactory(new DefaultActivityBehaviorFactory());
//
//        if (processEngineConfigurationConfigurer != null) {
//            processEngineConfigurationConfigurer.configure(conf);
//        }
//        return conf;
//    }
//
//    private void configureProcessDefinitionResources(
//            ProcessDefinitionResourceFinder processDefinitionResourceFinder,
//            SpringProcessEngineConfiguration conf) throws IOException {
//        List<Resource> procDefResources = processDefinitionResourceFinder
//                .discoverProcessDefinitionResources();
//        if (!procDefResources.isEmpty()) {
//            conf.setDeploymentResources(procDefResources.toArray(new Resource[0]));
//        }
//    }
//}